package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumInfoDetailVo;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public AlbumInfoDetailVo item(Long albumId) {

        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)) {
            throw new GuiguException(ResultCodeEnum.NOT_FIND_DATA);
        }

        AlbumInfoDetailVo albumInfoDetailVo = new AlbumInfoDetailVo();

        //根据专辑Id远程调用查询专辑基本信息
        CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfoById(albumId).getData();
            albumInfoDetailVo.setAlbumInfo(albumInfo);
            return albumInfo;
        },threadPoolExecutor);

        //根据专辑Id远程调用查询专辑统计信息
        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumInfoFeignClient.findAlbumStatByAlbumId(albumId).getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVo);
        },threadPoolExecutor);

        //根据专辑Id从前面查到的专辑基本信息中获得专辑3级分类号远程调用查询专辑分类信息
        CompletableFuture<Void> cf3 = cf1.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView = categoryFeignClient.findCategoryByC3Id(albumInfo.getCategory3Id()).getData();
            albumInfoDetailVo.setBaseCategoryView(baseCategoryView);
        },threadPoolExecutor);

        //根据专辑Id从前面查到的专辑基本信息中获得用户编号远程调用查询用户信息(专辑主播)
        CompletableFuture<Void> cf4 = cf1.thenAcceptAsync(albumInfo -> {
        UserInfo userInfo = userInfoFeignClient.findUserInfoById(albumInfo.getUserId()).getData();
        albumInfoDetailVo.setAnnouncer(userInfo);
        },threadPoolExecutor);

        //阻塞等待直至上面的四个异步线程任务执行完成后解除阻塞继续向下执行
        CompletableFuture.allOf(cf2,cf3,cf4).join();
        return albumInfoDetailVo;
    }
}
